Spring WS

您所在的位置:网站首页 XML WEB应用 Spring WS

Spring WS

2023-03-15 23:23| 来源: 网络整理| 查看: 265

Spring WS - 编写服务器端应用程序 ❮ 上一节 下一节 ❯

在本章中,我们将了解如何使用 Spring WS 创建 Web 应用程序服务器。

步骤 说明 1 在包 com.tutorialspoint 下创建一个名为 countryService 的项目,如 Spring WS - 第一个应用程序一章中所述。 2 按照以下步骤中的说明创建 countries.xsd、 Domain 域类、CountryRepository 和 CountryEndPoint。 3 更新 /WEB-INF 子文件夹下的 spring-ws-servlet.xml。 4 最后一步是为所有源文件和配置文件创建内容并导出应用程序,如下所述。 countries.xsd 实例 创建项目

让我们打开命令控制台,进入 C:\MVN 目录并执行以下 mvn 命令。

C:\MVN>mvn archetype:generate -DarchetypeGroupId = org.springframework.ws -DarchetypeArtifactId = spring-ws-archetype -DgroupId = com.tutorialspoint -DartifactId = countryService

Maven 将开始处理并创建完整的 Java 应用程序项目结构。

[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Stub Project (No POM) 1 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] Using property: groupId = com.tutorialspoint [INFO] Using property: artifactId = countryService Define value for property 'version': 1.0-SNAPSHOT: : [INFO] Using property: package = com.tutorialspoint Confirm properties configuration: groupId: com.tutorialspoint artifactId: countryService version: 1.0-SNAPSHOT package: com.tutorialspoint Y: : [INFO] ------------------------------------------------------------------------- --- [INFO] Using following parameters for creating project from Old (1.x) Archetype: spring-ws-archetype:2.0.0-M1 [INFO] ------------------------------------------------------------------------- --- [INFO] Parameter: groupId, Value: com.tutorialspoint [INFO] Parameter: packageName, Value: com.tutorialspoint [INFO] Parameter: package, Value: com.tutorialspoint [INFO] Parameter: artifactId, Value: countryService [INFO] Parameter: basedir, Value: C:\mvn [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] project created from Old (1.x) Archetype in dir: C:\mvn\countryService [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 35.989 s [INFO] Finished at: 2017-01-21T11:18:31+05:30 [INFO] Final Memory: 17M/178M [INFO] ------------------------------------------------------------------------

现在转到 C:/MVN 目录。 我们将看到一个名为 countryService(在 artifactId 中指定)的 java 应用程序项目。 更新 pom.xml。

pom.xml 实例 4.0.0 com.tutorialspoint.hr countryService war 1.0-SNAPSHOT countryService Spring-WS Application http://www.springframework.org/spring-ws countryService org.springframework.ws spring-ws-core 2.4.0.RELEASE jdom jdom 1.0 jaxen jaxen 1.1 wsdl4j wsdl4j 1.6.2 创建 Domain 域类

复制 C:\mvn\countryService\src\main\resources 文件夹中的 countries.xsd。 让我们打开命令控制台,进入 C:\mvn\countryService\src\main\resources 目录并执行以下 xjc 命令以使用 countries.xsd 生成域类。

C:\MVN\countryService\src\main\resources>xjc -p com.tutorialspoint countries.xsd

Maven 将开始处理并在 com.tutorialspoint 包中创建域类。

parsing a schema... compiling a schema... com\tutorialspoint\Country.java com\tutorialspoint\Currency.java com\tutorialspoint\GetCountryRequest.java com\tutorialspoint\GetCountryResponse.java com\tutorialspoint\ObjectFactory.java com\tutorialspoint\package-info.java

在 C:\mvn\countryService\src\main 文件夹中创建文件夹 java。 复制 C:\mvn\countryService\src\main\java 文件夹中的所有类。 创建 CountryRepository 和 CountryEndPoint 分别代表 country 数据库和 country 服务器。

CountryRepository.java 实例 package com.tutorialspoint; import java.util.ArrayList; import java.util.List; import org.springframework.beans.propertyeditors.CurrencyEditor; import org.springframework.stereotype.Component; import org.springframework.util.Assert; @Component public class CountryRepository { private static final List countries = new ArrayList(); public CountryRepository(){ initData(); } public void initData() { Country us = new Country(); us.setName("United States"); us.setCapital("Washington"); us.setCurrency(Currency.USD); us.setPopulation(46704314); countries.add(us); Country india = new Country(); india.setName("India"); india.setCapital("New Delhi"); india.setCurrency(Currency.INR); india.setPopulation(138186860); countries.add(india); Country uk = new Country(); uk.setName("United Kingdom"); uk.setCapital("London"); uk.setCurrency(Currency.GBP); uk.setPopulation(63705000); countries.add(uk); } public Country findCountry(String name) { Assert.notNull(name); Country result = null; for (Country country : countries) { if (name.trim().equals(country.getName())) { result = country; } } return result; } } CountryEndPoint.java 实例 package com.tutorialspoint.ws; import org.jdom.JDOMException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ws.server.endpoint.annotation.Endpoint; import org.springframework.ws.server.endpoint.annotation.PayloadRoot; import org.springframework.ws.server.endpoint.annotation.RequestPayload; import org.springframework.ws.server.endpoint.annotation.ResponsePayload; import com.tutorialspoint.Country; import com.tutorialspoint.CountryRepository; import com.tutorialspoint.GetCountryRequest; import com.tutorialspoint.GetCountryResponse; @Endpoint public class CountryEndPoint { private static final String NAMESPACE_URI = "http://tutorialspoint/schemas"; private CountryRepository countryRepository; @Autowired public CountryEndPoint(CountryRepository countryRepository) throws JDOMException { this.countryRepository = countryRepository; } @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest") @ResponsePayload public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) throws JDOMException { Country country = countryRepository.findCountry(request.getName()); GetCountryResponse response = new GetCountryResponse(); response.setCountry(country); return response; } } /WEB-INF/spring-ws-servlet.xml 实例 /WEB-INF/web.xml 实例 TutorialsPoint Country Service spring-ws org.springframework.ws.transport.http.MessageDispatcherServlet transformWsdlLocations true spring-ws /* 构建项目

让我们打开命令控制台。 转到 C:\MVN\countryService 目录并执行以下 mvn 命令。

C:\MVN\countryService>mvn clean package

Maven 将开始构建项目。

INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building countryService Spring-WS Application 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ countryService --- [INFO] Deleting C:\mvn\countryService\target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ countrySer vice --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ countryService --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent! [INFO] Compiling 4 source files to C:\mvn\countryService\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ co untryService --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory C:\mvn\countryService\src\test\resour ces [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ country Service --- [INFO] No sources to compile [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ countryService --- [INFO] No tests to run. [INFO] [INFO] --- maven-war-plugin:2.2:war (default-war) @ countryService --- [INFO] Packaging webapp [INFO] Assembling webapp [countryService] in [C:\mvn\countryService\target\count ryService] [INFO] Processing war project [INFO] Copying webapp resources [C:\mvn\countryService\src\main\webapp] [INFO] Webapp assembled in [5137 msecs] [INFO] Building war: C:\mvn\countryService\target\countryService.war [INFO] WEB-INF\web.xml already added, skipping [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 16.484 s [INFO] Finished at: 2017-01-28T09:07:59+05:30 [INFO] Final Memory: 19M/170M [INFO] ------------------------------------------------------------------------ 运行项目

创建源文件和配置文件后,将 countryService.war 文件导出到 Tomcat 的 webapps 文件夹中。

现在,启动 Tomcat 服务器并确保我们是否可以使用标准浏览器从 webapps 文件夹访问其他网页。向 URL 发出 POST 请求 - http://localhost:8080/countryService/ 并使用任何 SOAP 客户端发出以下请求。

实例 United States

您将看到以下结果。

实例 United States 46704314 Washington USD ❮ 上一节 下一节 ❯


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3